package com.jsptags.navigation.pager;

import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTag;

// Referenced classes of package com.jsptags.navigation.pager:
//			PageTagSupport, PagerTag

public final class PagesTag extends PageTagSupport implements BodyTag {

	private BodyContent bodyContent;
	private int page;
	private int lastPage;

	public final void setBodyContent(BodyContent bodycontent) {
		bodyContent = bodycontent;
	}

	public final int doStartTag() throws JspException {
		super.doStartTag();
		int i = pagerTag.getFirstIndexPage();
		lastPage = pagerTag.getLastIndexPage(i);
		page = i;
		return page > lastPage ? 0 : 2;
	}

	public final void doInitBody() throws JspException {
		setPageAttributes(page);
		page++;
	}

	public final int doAfterBody() throws JspException {
		try {
			if (page <= lastPage) {
				setPageAttributes(page);
				page++;
				return 2;
			}
			bodyContent.writeOut(bodyContent.getEnclosingWriter());
			return 0;
		} catch (IOException e) {
			throw new JspTagException(e.toString());
		}
	}

	public final int doEndTag() throws JspException {
		bodyContent = null;
		super.doEndTag();
		return 6;
	}

	public final void release() {
		bodyContent = null;
		super.release();
	}

	public PagesTag() {
		bodyContent = null;
		page = 0;
		lastPage = 0;
	}
}
